import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

/**
 * 导航传递参数到父页面
 */
class MyApp18 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "BottomNavigationWidget",
      theme: ThemeData(primarySwatch: Colors.lightBlue),
      home: FirstView(),
      debugShowCheckedModeBanner: false,
    );
  }
}

class FirstView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.brown,
      appBar: AppBar(
        title: Text(
          "first view",
          style: TextStyle(color: Colors.white, fontSize: 16.0),
        ),
        backgroundColor: Colors.deepPurpleAccent,
        leading: Container(),
        //这个值是AppBar 滚动时的融合程度，一般有滚动时默认是4.0，现在我们设置成0.0，就是和也main完全融合了。
        elevation: 0.0,
      ),
      body: Center(
        child: MaterialButton(
          child: Icon(
            Icons.navigate_next,
            color: Colors.white,
            size: 64,
          ),
          onPressed: () {
            Navigator.of(context).push(MyRoute(SecondView()));
          },
        ),
      ),
    );
  }
}

class SecondView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.purple,
      appBar: AppBar(
        title: Text(
          "second view",
          style: TextStyle(color: Colors.white, fontSize: 16.0),
        ),
      ),
      body: Center(
        child: MaterialButton(
          child: Icon(
            Icons.navigate_before,
            color: Colors.white,
            size: 64,
          ),
          onPressed: () {
            Navigator.of(context).pop();
          },
        ),
      ),
    );
  }
}

class MyRoute extends PageRouteBuilder {
  final Widget widget;

  MyRoute(this.widget)
      : super(
            // transitionDuration：设置动画持续的时间，建议再1和2之间。
            transitionDuration: const Duration(seconds: 1),
            pageBuilder: (BuildContext context, Animation<double> a1, Animation<double> a2) {
              return widget;
            },
            transitionsBuilder: (BuildContext context, Animation<double> a1, Animation<double> a2, Widget child) {
              // 4种动画效果

              //1,渐隐渐现过渡效果动画，主要设置opactiy（透明度）属性，值是0.0-1.0
              // return FadeTransition(
              //   //animate 动画的样式，一般使用动画曲线组件（CurvedAnimation）。
              //   //curve: 设置动画的节奏，也就是常说的曲线，Flutter准备了很多节奏，通过改变动画取消可以做出很多不同的效果。
              //   opacity: Tween(begin: 0.0, end: 1.0).animate(CurvedAnimation(parent: a1, curve: Curves.fastOutSlowIn)),
              //   child: child,
              // );

              //2,缩放动画
              // return ScaleTransition(scale: Tween(begin: 0.0,end: 1.0).animate(CurvedAnimation(parent: a1, curve: Curves.fastOutSlowIn)),child: child,);


              //3,旋转+缩放   思路是在一个路由动画里的child属性里再加入一个动画 让两个动画同时执行
              // return RotationTransition(turns: Tween(begin: 0.0,end: 1.0).animate(CurvedAnimation(parent: a1, curve: Curves.fastOutSlowIn)),
              // child: ScaleTransition(scale: Tween(begin: 0.0,end: 1.0).animate(CurvedAnimation(parent: a1, curve: Curves.fastOutSlowIn)),child: child,),
              // );

              //4,左右滑动动画
              return SlideTransition(position: Tween<Offset>(begin: Offset(-1.0,0.0),end:Offset( 0.0,0.0)).animate(CurvedAnimation(parent: a1,curve: Curves.fastOutSlowIn)),child: child,);
            });
}
